package com.hcrazy.manchester.app.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.BookMenu;
import com.hcrazy.manchester.app.pojo.Seller;
import com.hcrazy.manchester.app.services.BookMenuService;
import com.hcrazy.manchester.app.services.SellerService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/book")
public class BookMenuController {
		
	@Autowired
	private BookMenuService bookMenuService;
	
	@Autowired
	private SellerService sellerService;
	
	@RequestMapping(value="/menu",method=RequestMethod.GET)
	public void menu(Model model,@RequestParam(required=false) Long sellerId){
		if(null == sellerId)
			sellerId = 0L;
		model.addAttribute("sellerId", sellerId);
		getAllSellers(model);
	}
	
	/**
	 * 跳转到添加子分类页面
	 * 初始化数据
	 * @param menuParent
	 * @param model
	 */
	@RequestMapping(value="/addMenu",method=RequestMethod.GET)
	public void addMenu(@RequestParam int menuParent,@RequestParam long sellerId,Model model){
		//根据父分类id获取父分类对象
		try {
			BookMenu parentMenu = new BookMenu();
			if(menuParent == 0){
				parentMenu.setMenuId(0);
				parentMenu.setMenuName("无");
			}else{
				parentMenu = bookMenuService.getMenuById(menuParent);
			}
			model.addAttribute("parentMenu",parentMenu);
			BookMenu bookMenu = new BookMenu();
			model.addAttribute("bookMenu",bookMenu);
			
			Seller seller = sellerService.getSellerById(sellerId);
			model.addAttribute("seller", seller);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
	}
	
	/**
	 * 跳转到编辑分类页面
	 * @param menuId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editMenu",method=RequestMethod.GET)
	public String editMenu(@RequestParam int menuId,@RequestParam long sellerId,Model model){
		
		try {
			BookMenu bookMenu = bookMenuService.getMenuById(menuId);
			addMenu(bookMenu.getMenuParent(),sellerId,model);
			
			model.addAttribute("bookMenu",bookMenu);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
		return "/admin/book/addMenu";
	}
    
	/**
	 * 删除分类
	 * @param menuId
	 * @return
	 */
	@RequestMapping(value="/delMenu",method=RequestMethod.GET)
	public String delMenu(@RequestParam int menuId,@RequestParam long sellerId,Model model){
		try {
			String msg = bookMenuService.delBookMenu(menuId);
			if(null != msg){
				model.addAttribute("message", msg);
			}
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "redirect:/admin/book/menu?sellerId="+sellerId;
	}
	
	/**
	 * 提交分类信息
	 * @param menu
	 * @return
	 */
	@RequestMapping(value="/menu",method=RequestMethod.POST)
    public String addOrUpdateMenu(@ModelAttribute("form") BookMenu menu,
    		Model model){
		try {
			menu.setType(Constant.TYPE_BOOK_FOOD_MENU);
			bookMenuService.addOrUpdateBookMenu(menu);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/book/menu?sellerId="+menu.getSellerId();
    }
	
	/**
	 * 获取全部分类（树状结构）
	 * @return
	 */
	@RequestMapping(value="/menus",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<BookMenu> getAllMenus(@RequestParam long sellerId){
		
		try {
			return bookMenuService.getAllMenus(Constant.TYPE_BOOK_FOOD_MENU,sellerId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	private void getAllSellers(Model model){
		try {
			model.addAttribute("sellers",sellerService.getAllSellers());
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
}
